package cn.com.xf.utils.encode.impl;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.xf.utils.encode.EncodeService;

/**
 * 
 * @author Administrator
 * 
 */
public class EncodeServiceDESImpl implements EncodeService {

	private static Log logger = LogFactory.getLog(EncodeServiceDESImpl.class);
	private static String PRIVATE_KEY = "AABBCCDDMMNNLLOO";
	private static String DES = "DES";

	@Override
	public String encode(String input) {
		return this.desEncode(input.getBytes());
	}

	@Override
	public String encode(int input) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String encode(long input) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String decode2Str(String input) {
		return this.desDecode(input.getBytes());
	}

	@Override
	public int decode2Int(String input) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long decode2Long(String input) {
		// TODO Auto-generated method stub
		return 0;
	}

	private String desEncode(byte[] input) {
		SecureRandom sr = new SecureRandom();
		try {
			// 从原始密匙数据创建DESKeySpec对象
			DESKeySpec dks;
			dks = new DESKeySpec(PRIVATE_KEY.getBytes());
			// 创建一个密匙工厂，然后用它把DESKeySpec转换成一个SecretKey对象
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
			SecretKey securekey = keyFactory.generateSecret(dks);
			// Cipher对象实际完成加密操作
			Cipher cipher = Cipher.getInstance(DES);
			// 用密匙初始化Cipher对象
			cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
			// 现在，获取数据并加密
			// 正式执行加密操作
			byte[] encodeResult = cipher.doFinal(input);
			String result = byte2hex(encodeResult);
			return result;
		} catch (Exception e) {
			logger.error("desEncode::", e);
		}
		return null;
	}

	private String desDecode(byte[] input) {
		// DES算法要求有一个可信任的随机数源
		SecureRandom sr = new SecureRandom();
		try {
			// 从原始密匙数据创建一个DESKeySpec对象
			DESKeySpec dks = new DESKeySpec(PRIVATE_KEY.getBytes());
			// 创建一个密匙工厂，然后用它把DESKeySpec对象转换成
			// 一个SecretKey对象
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
			SecretKey securekey = keyFactory.generateSecret(dks);
			// Cipher对象实际完成解密操作
			Cipher cipher = Cipher.getInstance(DES);
			// 用密匙初始化Cipher对象
			cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
			// 现在，获取数据并解密
			// 正式执行解密操作
			byte[] decodeResult = cipher.doFinal(hex2byte(input));
			;
			return new String(decodeResult, 0, decodeResult.length);
		} catch (Exception e) {
			logger.error("desDecode::", e);
		}
		return null;
	}

	private static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}
		return hs.toUpperCase();
	}

	private static byte[] hex2byte(byte[] b) {
		if ((b.length % 2) != 0)
			throw new IllegalArgumentException("长度不是偶数");
		byte[] b2 = new byte[b.length / 2];
		for (int n = 0; n < b.length; n += 2) {
			String item = new String(b, n, 2);
			b2[n / 2] = (byte) Integer.parseInt(item, 16);
		}
		return b2;
	}

}
